import axios from 'axios'
import router from '@/router'

const axiosInstance = axios.create({
  // baseURL: 'http://192.168.2.137:18800',
  baseURL: 'http://49.234.30.204:9125',
  // timeout: 5000,
  headers: {
    'Content-Type': 'application/json',
  },
})

axiosInstance.interceptors.request.use(
  (config) => {
    config.headers.Authorization =
      localStorage.getItem('General_Token') || ''
    return config
  },
  (err) => {
    return Promise.reject(err)
  },
)

axiosInstance.interceptors.response.use(
  (res) => {
    const { code, message, data } = res.data
    if (code === 0) {
      return data
    } else {
      console.warn(message)
      return data
    }
  },
  (err) => {
    if (err.response) {
      const { status } = err.response
      // 如果是403权限不足
      if (status === 403) {
        console.warn('登录失败，请重新登录')
        // localStorage.removeItem('token') // 清除token
        localStorage.clear()
        router.replace('/login') // 跳转到登录页
      }
    } else {
      console.log(err)
    }

    return Promise.reject(err)
  },
)

export default axiosInstance
